#include "base_app.h"
#include <iostream>
#include "engine/utils/file_utils.h"
#include "engine/core/event_handler.h"


namespace kanon
{
    BaseApp::BaseApp()
        : logger(Logger::getInstance())
    {
        logger.setLogFile("log.info");
        logger.setLogLevel(LogLevel::DEBUG);

        // initialize window
        WindowInfo info;
        info.width = 1280;
        info.height = 720;
        info.title = "Kanon Engine";
        window = std::make_shared<Window>(info);
        window->Initialize();
    }

    BaseApp::~BaseApp()
    {
    }

    void BaseApp::Initialize()
    {
        actor_system = std::make_shared<ActorSystem>();

        ui_layer = std::make_shared<UILayer>();
        ui_layer->Initialize(window->GetHandle());

        RegisterEvents();
    }

    void BaseApp::Run()
    {
        double prev_time = window->GetFrameTime();

        while (!window->WindowClosed())
        {
            window->Update();
            double current_time = window->GetFrameTime();
            float dt = static_cast<float>(current_time - prev_time);
            prev_time = current_time;
            Update(dt);
            Render();
            window->SwapBuffers();
        }
    }

    void BaseApp::Update(float deltaTime)
    {
        actor_system->Update(deltaTime);
    }
}
